package com.jt.controller;

import com.jt.service.FileService;
import com.jt.vo.ImageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class FileController {
    @Autowired
    FileService fileService;
    /**
     * url地址：http://localhost/file.jsp
     * 参数：文件信息fileImage
     * 放回置String
     */
    @RequestMapping("/file")
    public String file(MultipartFile fileImage) throws IOException {
        //1.获取图片名称
        String name = fileImage.getOriginalFilename();
        //2.获取图片文件目录
        String filePirPath = "D://图片";
        //3.创建目录
        File fileDir = new File(filePirPath);
        //判断是否有目录，没有则创建
        if (!fileDir.exists()){
            fileDir.mkdir();
        }
        //4.生成文件全路径
        String filePath = filePirPath + "/" + name;
        File imageFile = new File(filePath);
       // 5.实现文件的上传
        fileImage.transferTo(imageFile);
        return "文件上传成功";
    }
    /**
     * 需求:实现文件上传的操作
     * url:http://localhost:8091/pic/upload?dir=image
     * 参数:uploadFile
     * 返回值:ImageVo
     */
    @RequestMapping("/pic/upload")
    public ImageVo upload(MultipartFile uploadFile){
        return fileService.upload(uploadFile);
    }
}
